//
//  MainHeaderview.m
//  Bettle
//
//  Created by panhongliu on 2017/1/5.
//  Copyright © 2017年 panhongliu. All rights reserved.
//

#import "MainHeaderview.h"
#import "MainHeaderCollectionViewCell.h"
#import "MainHeaderOtherCollectionViewCell.h"
@implementation MainHeaderview


- (instancetype)initWithFrame:(CGRect)frame
{
    if (self=[super initWithFrame:frame]) {
        self.backgroundColor=[UIColor redColor];
        flowLayout= [[UICollectionViewFlowLayout alloc]init];
        
       // flowLayout.scrollDirection =UICollectionViewScrollDirectionVertical;
        flowLayout.minimumLineSpacing = 0;
        
        
        self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, self.height) collectionViewLayout:flowLayout];
        
        //    flowLayout.itemSize = CGSizeMake((SCREEN_WIDTH-30)/2, (SCREEN_WIDTH-30)/2+47);
        
        UINib *nib = [UINib nibWithNibName:@"MainHeaderCollectionViewCell"
                                    bundle: [NSBundle mainBundle]];
        [self.collectionView registerNib:nib forCellWithReuseIdentifier:@"MainHeaderCollectionViewCell"];
        
        UINib *nib2 = [UINib nibWithNibName:@"MainHeaderOtherCollectionViewCell"
                                     bundle: [NSBundle mainBundle]];
        [self.collectionView registerNib:nib2 forCellWithReuseIdentifier:@"MainHeaderOtherCollectionViewCell"];
        
        self.collectionView.backgroundColor=[UIColor whiteColor];
    
        flowLayout.scrollDirection =UICollectionViewScrollDirectionHorizontal;
        self.collectionView.delegate = self;
        self.collectionView.dataSource = self;
        [self addSubview:self.collectionView];
        

    }
    return self;
    
    
}

- (void)awakeFromNib {
    [super awakeFromNib];
    NSLog(@"ssd");
  }

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section

{
    
    return 5;
    
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
       return 10;
}

-(void)cellDataArray:(NSArray *)array;
{
    
    self.dataArray=array;
    [self.collectionView reloadData];
    
}


- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    WeakSelf(weakSelf, self);
        if (self.type==2) {
      
             MainHeaderCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"MainHeaderCollectionViewCell" forIndexPath:indexPath];
            cell.headermodel = self.dataArray[indexPath.row];
             return cell;

        
        }
        else{
              MainHeaderOtherCollectionViewCell *cell= [collectionView dequeueReusableCellWithReuseIdentifier:@"MainHeaderOtherCollectionViewCell" forIndexPath:indexPath];
            if (self.type == 2) {
                cell.model = self.dataArray[indexPath.row];

            }else{
            cell.model2= self.dataArray[indexPath.row];
            }
            return cell;

        }
    
    
}

//定义每个UICollectionView 的大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
        return CGSizeMake(self.width-80*SHIJI_WIDTH,self.height);
    
  
}
//定义每个UICollectionView 的 margin
-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    
        return UIEdgeInsetsMake(0, 0,0, 20);
        
   
    
}


@end
